package cn.bone.protobuf.demo;

import com.alibaba.fastjson.JSON;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

/**
 * Created by MD-01 on 2017/2/24.
 */
public class Main {

    public static void main(String[] args){
        User user = new User();
        user.setId(1);
        user.setName("戴泽");
        user.setAge(20);
        User.Address address = new User.Address();
        address.setCity("武汉");
        address.setRoad("民族大道");
        address.setTown("洪山区");
        user.setAddress(address);

        Schema<User> schema = RuntimeSchema.getSchema(User.class);
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);

        byte[] bytes = ProtostuffIOUtil.toByteArray(user, schema, buffer);
        System.out.println(bytes.length);

        User u2 = new User();
        ProtostuffIOUtil.mergeFrom(bytes,u2,schema);
        System.out.println(JSON.toJSONString(u2));
    }
}
